package com.sbl.bajw.pc.service;

import java.util.HashMap;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sbl.bajw.common.MyBatisDao;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.mapper.UserRoleDao;

@Service
public class UserRoleService {
	
	@Resource
	private UserRoleDao urdao;
	
	@Resource 
	private MyBatisDao gridDao;
	
	public String selectUserRoleList(){
		String id = RequestUnit.getString("user_id");
		HashMap<String, Object> parm = new HashMap<String, Object>();
		parm.put("user_id", id);
		String result = gridDao.selectPage(UserRoleDao.class, parm, "selectUserRoleList");
		return result;
	}
	
	@Transactional
	public void addRoleToUser(){
		String roleIdsStr = RequestUnit.getString("roleids");
		String userId = RequestUnit.getString("user_id");
		if(roleIdsStr == null||userId == null){
			return;
		}
		urdao.deleteRoleUserByUser(userId);
		String[] roleids = roleIdsStr.split(",");
		for(int i=0;i<roleids.length;i++){
			HashMap<String, String> parm = new HashMap<String, String>();
			parm.put("role_id", roleids[i]);
			parm.put("user_id", userId);
			urdao.insertUserRole(parm);
		}
	}
}
